package www.ld.com.filter;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import www.ld.com.common.SwaggerConstants;

@Component
public class SwaggerHeaderFilter  extends AbstractGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getURI().getPath();
            if (!StringUtils.endsWithIgnoreCase(path, SwaggerConstants.URI)) {
                return chain.filter(exchange);
            }
            //String basePath = path.substring(0, path.lastIndexOf(Settings.URI));
           // ServerHttpRequest newRequest = request.mutate().header(Settings.HEADER_NAME, "").build();
            ServerWebExchange newExchange = exchange.mutate().request(request.mutate().build()).build();
            return chain.filter(newExchange);
        };
    }
}
